package com.bdqn.ch03thymeleaf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.ch03thymeleaf.entity.Account;
import com.bdqn.ch03thymeleaf.entity.TransactionRecord;
import com.bdqn.ch03thymeleaf.service.TransferService;
import com.bdqn.ch03thymeleaf.utils.Md5Util;
import com.bdqn.ch03thymeleaf.utils.R;
import com.bdqn.ch03thymeleaf.utils.RedisUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Controller
@RequestMapping("/transfer")
public class TransferController {
    @Autowired
   private TransferService transferService;

    public static String generateRandomString(int length) {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }

    // 登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "/login";
    }

    // 处理登录请求
    @PostMapping("/login")
    public String login(@RequestParam String cardno, @RequestParam String password, HttpSession session, Model model) {
        Optional<Account> accountOptional = Optional.ofNullable(transferService.getLogin(cardno, password));
        if (accountOptional.isPresent()) {
            session.setAttribute("account", accountOptional.get());
            return "redirect:/list";
        } else {
            model.addAttribute("error", "卡号或密码错误");
            return "/login";
        }
    }

    @RequestMapping("/getAccount")
    public R getAccount(@RequestParam String cardno) {
        Account account = transferService.getAccount(cardno);
        if (account == null) {
            return R.fail();
        } else {
            return R.ok(account);
        }
    }

    @RequestMapping("/update")
    public R update(@RequestBody Account account) {
        int num = transferService.updateTransfer(account);
        if (num < 0) {
            return R.fail();
        } else {
            return R.ok(account);
        }
    }

    @RequestMapping("/transfer")
    public R transfer(@RequestParam String fromCard, @RequestParam String toCard, @RequestParam BigDecimal amount) throws Exception {
        try {
            transferService.transfer(fromCard, toCard, amount);
            return R.ok();
        } catch (Exception e) {
            return R.fail();
        }
    }

    @RequestMapping("/add")
    public R add(@RequestBody TransactionRecord transactionRecord) {
        int num = transferService.addAccount(transactionRecord);
        if (num < 0) {
            return R.fail();
        } else {
            return R.ok(transactionRecord);
        }
    }


    @RequestMapping("/page")
    public R page(@RequestParam String cardno,
                  @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                  @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                  @RequestParam(value = "current", defaultValue = "1") Integer current,
                  @RequestParam(value = "size", defaultValue = "3") Integer size) {
        Page page = transferService.getPage(cardno, startDate, endDate, current, size);
        if (page.getRecords() == null || page.getRecords().size() == 0) {
            return R.fail();
        } else {
            return R.ok(page);
        }
    }
}
